package proxy;

import org.junit.Test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class App {
    @Test
    public void test01() {
        Performer performer = new Songzhe();
        performer.perform();
    }

    @Test
    public void test02() {
        //1. 创建目标对象
        Performer performer = new BaoQiang();

        //2. 编写增强逻辑
        InvocationHandler invocationHandler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object obj = null;
//                增强功能
                System.out.println("让客户交钱");
                System.out.println("安慰宝强，你去表演，家里我来照顾");
//               调用目标对象的原始方法
                obj = method.invoke(performer, args);
                return obj;
            }
        };

        //3. 创建代理对象(使用jdk将上面的目标对象和增强逻辑拼接在一起的过程)
        Performer instance = (Performer) Proxy.newProxyInstance(
                performer.getClass().getClassLoader(),
                performer.getClass().getInterfaces(),
                invocationHandler
        );
        //4. 让代理对象去工作
        instance.perform();
    }
}
